package com.hong.qrpc.register.nacos;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.hong.qrpc.pojo.RegisterCenterConfig;
import com.hong.qrpc.register.Register;
import lombok.extern.slf4j.Slf4j;

/**
 * 用于进行Nacos的服务发现与注册与监听等操作
 * @Author HQT
 * @Date 2022/7/23 11:11
 */
@Slf4j
public class NacosRegister implements Register {
    @Override
    public void registerService(RegisterCenterConfig registerCenterConfig) {
        NamingService namingService = null;
        try {
            namingService = NamingFactory.createNamingService(registerCenterConfig.getRegisterCenterAddr());
            namingService.registerInstance(registerCenterConfig.getServerName(), registerCenterConfig.getServerIp(), registerCenterConfig.getServerPort());
            log.info("already register to register center:"+registerCenterConfig.getRegisterCenterAddr());
        } catch (NacosException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("注册服务异常"+e.getMessage()+" parameter is "+ registerCenterConfig);
        }
    }
}
